package com.example.learnloadmanager;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {

    private SQLHelper sqlHelper = null;
    public final static String AUTHORITY = "com.app.myContentProvider";
    private final static int TABLE_A_CODE = 1;
    public final static Uri MyContentProvider_Uri = Uri.parse("content://" + AUTHORITY + "/" + SQLHelper.TABLE_A_NAME); 

    private static final UriMatcher mather = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        mather.addURI(AUTHORITY, SQLHelper.TABLE_A_NAME, TABLE_A_CODE);
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        sqlHelper = new SQLHelper(this.getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = sqlHelper.getWritableDatabase();
        Cursor cursor = db.query(SQLHelper.TABLE_A_NAME, projection, selection,
                selectionArgs, null, null, null);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        SQLiteDatabase db = sqlHelper.getWritableDatabase();
        int flag = mather.match(uri);

        switch (flag) {
        case TABLE_A_CODE:
            long id = db.insert(SQLHelper.TABLE_A_NAME, null, values);
            return ContentUris.withAppendedId(uri, id);

        default:
            break;
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

}
